package com.yyq.adms.gf.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.gf.bpo.OwnLawBpo;
import com.yyq.adms.gf.pojo.OwnLaw;
import com.yyq.adms.sysmanage.pojo.ExcelBean;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;

@Controller("ownLawAction")
@Scope("prototype")
public class OwnLawAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private OwnLaw po=new OwnLaw();
	private OwnLawBpo bpo;
	
	
	@Resource(name="ownLawBpo")
	public void setBpo(OwnLawBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	public OwnLaw getPo() {
		return po;
	}

	public void setPo(OwnLaw po) {
		this.po = po;
	}

	@Override
	public String execute() throws Exception {
		super.setObj(po);
		super.setBpo(bpo);
		String ret = super.execute();
		po = (OwnLaw)getObj();
		return ret;
	}
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from OwnLaw order by lawYear desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+") order by obj.lawYear desc";
		}
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		return ActionType.ACTION_DATA_LIST;
	}

	public String exportAll(){
		String hql = getExportHql("lawYear");
		List list = bpo.search(hql);
		String temp = "";
		ExcelBean eb = null;
		List datas = new ArrayList();
		for(int i=0;i<list.size();i++){
			OwnLaw obj = (OwnLaw)list.get(i);
			processObjLevels(obj);
			if(!temp.equals(obj.getLawYear())){
				temp = obj.getLawYear();
				if(eb != null){
					datas.add(eb);
				}
				eb = new ExcelBean(obj.getLawYear());
			}
			eb.getData().add(obj);
			if(i == list.size()-1){
				datas.add(eb);
			}
		}
		Map beans = new HashMap();
		beans.put("datas", datas);
		beans.put("isGlobal", isSentDepRight(null));
		exportXls(beans,"获批工法批量导出数据",null);
		return null;
	}

	private void processObjLevels(OwnLaw obj) {
		if("1".equals(obj.getLevels())){
			obj.setLevels("国家级");
		}else if("2".equals(obj.getLevels())){
			obj.setLevels("广东省");
		}else if("3".equals(obj.getLevels())){
			obj.setLevels("交通运输部");
		}else if("4".equals(obj.getLevels())){
			obj.setLevels("中交股份");
		}else if("5".equals(obj.getLevels())){
			obj.setLevels("四航局");
		}
	}
}
